//
//  TestViewController.swift
//  FlutterBoostIOSDemo
//
//  Created by wangliang on 2024/4/23.
//

import UIKit
import SnapKit
import flutter_boost

class TestViewController: UIViewController {
    
    var pageName: String?
    var params:[AnyHashable: Any]? = [:]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white

        self.title = "Test Page"
    
        let paramsLabel = UILabel()
        paramsLabel.text = "启动参数: \(params)"
        paramsLabel.font = .systemFont(ofSize: 15)
        paramsLabel.textColor = .gray
        view.addSubview(paramsLabel)
        paramsLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(100)
            make.leading.equalToSuperview().offset(20)
        }
        
        let button = UIButton(type: .system)
        button.setTitle("关闭页面返回参数", for: .normal)
        button.addTarget(self, action: #selector(backAndWithResult), for: .touchUpInside)
        view.addSubview(button)
        button.snp.makeConstraints { make in
            make.top.equalTo(paramsLabel.snp.bottom).offset(10)
            make.leading.equalToSuperview().offset(20)
        }

        let button1 = UIButton(type: .system)
        button1.setTitle("原生页面 -> Flutter 返回数据", for: .normal)
        button1.addTarget(self, action: #selector(jumpPage1), for: .touchUpInside)
        view.addSubview(button1)
        button1.snp.makeConstraints { make in
            make.top.equalTo(button.snp.bottom).offset(10)
            make.leading.equalToSuperview().offset(20)
        }
        // Do any additional setup after loading the view.
    }
    
    @objc func backAndWithResult() {
        let result: [String : Any] = ["msg": "This is from native iOS content", "int": 111, "bool": true]
        if pageName?.isEmpty == false {
            //这里pageName是你push的这个原生的pageName，而不是上一个flutter页面的pageName
            //这句话并不会退出页面
            FlutterBoost.instance().sendResultToFlutter(withPageName: pageName, arguments: result)
            self.navigationController?.popViewController(animated: true)
        }
    }

    @objc func jumpPage1() {
        let options = FlutterBoostRouteOptions()
        options.pageName = FlutterBoostAgent.flutterMainPage
        options.arguments = ["title": "MainPage"]
        options.onPageFinished = { result in
            NSLog("WLTest [Native -> Flutter] 返回数据: \(result)")
        }
        FlutterBoostAgent.shared.openFlutterRoute(self.navigationController, options: options)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
